package com.shangcheng.server.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.shangcheng.dubbo.api.SmsHomeNewProductApi;
import com.shangcheng.model.domain.SmsHomeNewProduct;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.server.exception.BusinessException;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * @author shiqiang
 * @version 1.0
 * @date 2022/5/12 21:41
 */
@Service
public class SmsHomeNewProductService {
    @DubboReference
    private SmsHomeNewProductApi smsHomeNewProductApi;
    /**
     * 批量添加首页新品
     * @param homeNewProductList 新品集合
     */
    public void createNewPro(List<SmsHomeNewProduct> homeNewProductList) {
        //根据品牌名称查询数据库
        List<SmsHomeNewProduct> homeNewProducts= smsHomeNewProductApi.findByName(homeNewProductList);
        if (!ObjectUtils.isEmpty(homeNewProducts)){
            //如果存在,抛出异常
            List<String> productNames = CollUtil.getFieldValues(homeNewProducts, "productName", String.class);
            throw new BusinessException(productNames.toString()+"已存在");
        }
        smsHomeNewProductApi.createNewPro(homeNewProductList);
    }

    /**
     * 批量删除首页新品
     * @param ids 新品ids
     */
    public void deleteNewPro(Integer[] ids) {
        smsHomeNewProductApi.deleteNewPro(ids);
    }

    /**
     * 分页查询首页新品
     * @param pageNum
     * @param pageSize
     * @param productName
     * @param recommendStatus
     * @return
     */
    public PageResult newProList(Integer pageNum, Integer pageSize, String productName, Integer recommendStatus) {
        IPage<SmsHomeNewProduct> page = smsHomeNewProductApi.newProList(pageNum, pageSize, productName, recommendStatus);
        return new PageResult(pageNum,pageSize,Convert.toInt(page.getTotal()),page.getRecords());
    }

    /**
     * 批量修改首页新品推荐状态
     * @param ids
     * @param recommendStatus
     */
    public void updateRecommendStatus(Integer[] ids, Integer recommendStatus) {
        smsHomeNewProductApi.updateRecommendStatus(ids,recommendStatus);
    }

    /**
     * 修改首页新品排序
     * @param id
     * @param sort
     */
    public void updateSort(Integer id, Integer sort) {
        smsHomeNewProductApi.updateSort(id,sort);
    }

}
